<script type="text/javascript">
	var SeaOptions = {
		Dialog: {
			method: 'tmpl',
			onshow: function(){
				var config = {
					rules: {
						title: { required: true, minlength: 5},
						catid: { required: true},
						displayorder: { required: true},
						score: { required: true},
						type: { required: true},
						isneed: { required: true},
						"answer[]": { required: true},
						istrue: { required: true}
					},
					messages:{
						title: { required: '试题题目不能为空！', minlength: '试题题目不能少于5个字符！'},
						catid: "试题分类必须选择！",
						displayorder: "试题显示顺序必须填写！",
						score: "试题所占分数必须填写！",
						type: "试题类型必须选择！",
						isneed: "必须设定是否为综合考试必考试题！",
						"answer[]": "试题可选答案必须填写！",
						istrue: "正确答案必须设定"
					},
					async: false,
					dialog: this,
					callback: function(){
						location.reload();
					}
				};
				jQ('[dialog-id='+this.id+']>form').seaModule('Validator', config);
			},
			cancelValue: '取消关闭',
			cancel: function(){},
			okValue: '确定提交',
			ok: function(){
				jQ('[dialog-id='+this.id+']>form').submit();
				return false;
			},
			callback: function(dialog){
				var form = jQ('[dialog-id='+dialog.id+']'),
					kindeditor = {
						minWidth: '410px',
						width: '420px',
						height: '180px',
						items : ['wordpaste', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'underline','|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist', '|','subscript',	'superscript', 'image', 'link']
					};
				function questionInput(i, answer, check){
					var html = '<li><a href="javascript:void(0);" class="delLi">[-]</a> ' +
								'<input name="answer[]" type="text" value="'+answer+'" /> ' +
								'<input type="radio" name="istrue" value="'+i+'" id="istrue'+i+'" '+check+' />' +
								'<label for="istrue'+i+'"> 正确答案</label></li>';
					jQ('#answerUl').append(html);
				}
				function defaultInput(displayorder, score, type, isneed){
					jQ('[name=displayorder]:input', form).val(displayorder);
					jQ('[name=score]:input', form).val(score);
					jQ('input[name=type][value='+type+']', form).attr('checked', 'checked');
					jQ('input[name=isneed][value='+isneed+']', form).attr('checked', 'checked');
				}
				if(dialog.tmpl.ac == 'Add'){
					for(var i=0; i<2; i++){
						questionInput(i, '', '');
					}
					jQ('[name=catid]').change(function(){
						var displayorder = jQ(this).find('option:selected').attr('order');
						defaultInput(displayorder, {$score}, '单选题', 0);
					})
					jQ('[name=description]', form).seaModule('seaKindeditor', kindeditor);
					jQ('[name=apply_action]:input', form).val('Add');
				}else{
					jQ.post('/plugin.php?id=iexam:ajax',{'ac': dialog.tmpl.ac, 'eid': dialog.tmpl.eid},
						function(json){
							jQ('[name=title]:input', form).val(json.title);
							jQ('[name=catid]>option[value='+json.catid+']', form).attr('selected', 'selected');
							defaultInput(json.displayorder, json.score, json.type, json.isneed);
							jQ.each(json.question, function(i, d){
								var check = (d.istrue)?'checked="checked"':'';
								questionInput(i, d.answer, check);
							});
							if(json.description) jQ('[name=description]', form).text(json.description);
							jQ('[name=description]', form).seaModule('seaKindeditor', kindeditor);
							jQ('[name=apply_action]:input', form).val('Edit');
							jQ('form', form).append('<input type="hidden" name="eid" value="'+json.eid+'" />');
							jQ('form', form).append('<input type="hidden" name="tid" value="'+json.tid+'" />');
							jQ('form', form).append('<input type="hidden" name="pid" value="'+json.pid+'" />');
					});
				}
				jQ('[dialog-id='+dialog.id+']').coffee({
					click: {
						'.addLi': function(){
							var index = jQ('td>ul>li', jQ(this).closest('tr')).length;
							questionInput(index, '', '');
						},
						'.delLi': function(){
							jQ(this).closest('li').remove();
							jQ('.delLi').each(function(i, d){
								jQ(d).closest('li').find('[name=istrue]:input').val(i);
							})
						},
						'[data-sea=RadioCheckbox]:eq(0)>input': function(){
							if(jQ(this).is(':checked') && jQ(this).val() == '判断题'){
								jQ('#answerUl').closest('tr').find('th>a').hide();
								jQ('#answerUl>li:eq(0)').find('a').hide();
								jQ('#answerUl>li:eq(1)').find('a').hide();
								jQ('#answerUl>li:eq(0)').find('input:eq(0)').val('√').attr('readonly', 'readonly');
								jQ('#answerUl>li:eq(1)').find('input:eq(0)').val('×').attr('readonly', 'readonly');
							}else{
								jQ('#answerUl').closest('tr').find('th>a').show();
								jQ('#answerUl>li:eq(0)').find('a').show();
								jQ('#answerUl>li:eq(1)').find('a').show();
								jQ('#answerUl>li:eq(0)').find('input:eq(0)').val('').removeAttr('readonly');
								jQ('#answerUl>li:eq(1)').find('input:eq(0)').val('').removeAttr('readonly');
							}
						}
					}
				})
			}
		}
	}
</script>
<style>
	Form input[type=text]{height:20px; width: 410px; padding-left: 5px;}
	Form li{height:28px; line-height: 28px;}
	Form li>input[type=text]{width: 310px;}
</style>
<div class="bm bw0">
	<ul class="tb cl">
		<li class="a"><a href="#">全部</a></li>
		<li class="o y"><a href="#actionDialog" data-sea="artDialog" data-config="Dialog" data-tmpl="ac:'Add'">添加新的考试试题</a></li>
	</ul>
	<table class="dt mtm">
		<thead>
			<tr>
				<th width="20">&nbsp;</th>
				<th>考试题目</th>
				<th width="100">所属分类</th>
				<th width="70">考题类型</th>
				<th width="70">操作</th>
			</tr>
		</thead>
		<tbody id="seajPageItem">
			<!--{loop $list $key $val}-->
			<tr>
				<td><input type="checkbox" value="{$val[eid]}" name="eids[]" class="pc"></td>
				<td>
					<a href="forum.php?mod=viewthread&tid={$val[tid]}" title="{$val[title]}" target="_blank" style="">{$val[title]}</a>
				</td>
				<td>{$val[catname]}</td>
				<td>{$val[type]}</td>
				<td>
					<a href="#actionDialog" data-sea="artDialog" data-config="Dialog" data-tmpl="ac:'Edit',eid:'{$val[eid]}'" title="编辑【{$val[title]}】试题">编辑</a>
					<a href="#actionDialog" data-sea="artDialog" data-config="Dialog" data-tmpl="ac:'Del',eid:'{$val[eid]}'" title="删除【{$val[title]}】试题">删除</a>
				</td>
			</tr>
			{/loop}
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5">
					<div class="pg" data-sea="jPage" data-option='startPage:1,perPage:10,navLi:false'></div>
				</td>
			</tr>
		</tfoot>
	</table>
</div>
<script id="actionDialog" type="text/x-jquery-tmpl">
<div>
<form action="./plugin.php?id=iexam:ajax" method="ajax">
	<div class="c">
		<div class="bm bw0">
			<table class="dt mtm">
				<tbody>
					<tr>
						<th width="80">试题题目</th>
						<td colspan="3"><input name="title" type="text" value=""/></td>
					</tr>
					<tr>
						<th width="80">试题分类</th>
						<td colspan="3">
							<select name="catid" class="ps vm">
								<option value="">--请选择试题分类--</option>
								<!--{loop $category[select] $key $val}-->
								<option value="{$val[catid]}" order="{$val[order]}">{$val[catname]}</option>
								<!--{/loop}-->
							</select>
						</td>
					</tr>
					<tr>
						<th width="80">显示顺序</th>
						<td><input name="displayorder" type="text" value="" style="width: 120px;"/></td>
						<th width="80">试题分数</th>
						<td><input name="score" type="text" value="" style="width: 120px;"/></td>
					</tr>
					<tr>
						<th width="80">试题类型</th>
						<td>
							<input type="radio" id="type1" name="type" value="单选题">
							<label for="type1"> 单选题</label>
							<input type="radio" id="type2" name="type" value="判断题">
							<label for="type2"> 判断题</label>
						</td>
						<th width="80">综合必考</th>
						<td>
							<input type="radio" id="need1" name="isneed" value="0" />
							<label for="need1"> 非必考</label>
							<input type="radio" id="need2" name="isneed" value="1" />
							<label for="need2"> 必考题</label>
						</td>
					</tr>
					<tr>
						<th width="80">试题答案<a href="javascript:void(0);" class="addLi">[+]</a></th>
						<td colspan="3">
							<ul id="answerUl"></ul>
						</td>
					</tr>
					<tr>
						<th width="80">试题解析</th>
						<td colspan="3"><textarea name="description"></textarea></td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
	<input type="hidden" name="apply_action">
	<input type="hidden" name="formhash" value="{FORMHASH}">
</form>
</div>
</script>